/*INIT*/
$(document).ready(function() {
	loadAddress();
	$('.partr #showEditTable').click(function(){
		$('#editAddrTable #textAddrId').val('');
		$('#editAddrTable #textAddr').val('');
		$('#editAddrTable #textPhone').val('');
		$('#editAddrTable').show();
	});
});

var addressAction = {
	init: function() {
		$('.addressList [name=q_address] .c4 .sel_addr').click(addressAction.sel);
		$('.addressList [name=q_address] .c4 .del_addr').click(addressAction.del);
		$('.addressList [name=q_address] .c3 .set_def').click(addressAction.setDefault);
		$('.partr #showEditTable').click(addressAction.showTable);
		$('#editTable .submit').click(addressAction.submit);
		var x = $('.addressList [name=q_address]').first();
	},

	sel: function() {
		//get this item
		var x = $(this).parent().parent();
		$('#editAddrTable #textAddrId').val('');
		$('#editAddrTable #textAddr').val(x.children('.c1').text());
		$('#editAddrTable #textPhone').val(x.children('.c2').text());
		$('#editAddrTable').show();
	},

	del: function() {
		$(this).parent().parent().remove();
	},

	setDefault: function() {
			
	},

	edit: function() {
		
	},

	showTable: function() {
		$('#editAddrTable #textAddrId').val('');
		$('#editAddrTable #textAddr').val('');
		$('#editAddrTable #textPhone').val('');
		$('#editAddrTable').show();
	},

	submit: function() {
		
	}
};

function loadAddress() {
	$.ajax({
        url : getRootPath() + '/customerajax/changeaddress!load.action',
        cache : false, 
        async : true,
        type : "POST",
        dataType : 'json',
        beforeSend	: function(xmlHttpRequest){
			//开启提交动画
		},
        success : function (result){
		    //关闭提交动画，响应成功请求
        	updateAll(result);
        },
        error	:	function(){
        	//关闭提交动画，响应失败请求
        	alert("error");
        }
    });
}
//delete address
function delAddr(ele,addressId) {
	$(ele).parentsUntil('#showAddrList').remove();
	$.ajax({
        url : getRootPath() + '/customerajax/changeaddress!delAddress.action',
        data :{id:addressId},
        cache : false, 
        async : true,
        type : "POST",
        dataType : 'json',
        beforeSend	: function(xmlHttpRequest){
			//开启提交动画
		},
        success : function (result){
		    //关闭提交动画，响应成功请求        	
        	updateAll(result);
        },
        error	:	function(){
        	//关闭提交动画，响应失败请求
        	alert("error");
        }
    });
}
// add address
function addEditAddr() {
	var addrId = $('#textAddrId').val();
	var addr = $('#textAddr').val();
	var phone = $("#textPhone").val();
	if(addrId == "") {
		$.post(getRootPath() + '/customerajax/changeaddress!addAddress.action', 
					{editAddr:addr, editPhone:phone}, 
					function(data) {
						$("#editAddrTable").hide();
						updateAll(data);
		});
	} else {
		$.post(getRootPath() + '/customerajax/changeaddress!editAddress.action', 
				{id:addrId, editAddr:addr, editPhone:phone}, 
				function(data) {
					$("#editAddrTable").hide();
					updateAll(data);
		});
	} 

}
// set default address
function setDefault(addressId){
	$.ajax({
        url : getRootPath() + '/customerajax/changeaddress!setDefault.action',
        data :{id:addressId},
        cache : false, 
        async : true,
        type : "POST",
        dataType : 'json',
        beforeSend	: function(xmlHttpRequest){
			//开启提交动画
		},
        success : function (result){
		    //关闭提交动画，响应成功请求        	
        	updateAll(result);
        	//$('#form_addAddress .submit').click(function(){$('#form_addAddress').submit();});
        },
        error	:	function(){
        	//关闭提交动画，响应失败请求
        	alert("error");
        }
    });
}
//edit address
function editAddr(ele,addressId){
		
	$('#textAddrId').val(addressId);	

	$('#editAddrTable #textAddr').val($(ele).parentsUntil('#showAddrList').find('.c1').text());
	$('#editAddrTable #textPhone').val($(ele).parentsUntil('#showAddrList').find('.c2').text());
	
	$('#editAddrTable').show();
	
}
function updateAll(data) {
	//clear all.
	$('.addressList .item').unbind('mouseenter');
	$('.addressList .item').unbind('mouseleave');
	$('.partr .section1 .item').remove();	
	//get global variable
	var tHead = "<div id=\"addrHead\" class=\"item lh\"><div class=\"c1\">送餐地址</div><div class=\"c2\">联系电话</div><div class=\"c4\">操作</div></div>";
	$('#showAddrList').html(tHead);
	// no default address, but address list is not empty.
	var item = null;
	if(data.addrs.length != 0) {
		$.each(data.addrs, function(i){
			// set default  address style
			if(data.defaultAddress[i] == 1){
			 item = "<div class=\"item id\" name='q_address'>" +
			   "<div class=\"default\"><div class=\"l\"></div><div class=\"r\"></div></div>"+
			   "<div class=\"a\"></div>" + 
			   "<div class=\"c1\">"+data.addrs[i]+"</div>" +
			   "<div class=\"c2\">"+data.phones[i]+"</div>" +
			   "<div class=\"c3b\">默认地址</div>" +
			   "<div class=\"c4\"><a class=\"sel_addr\" onclick=\"editAddr(this,'"+data.ids[i]+"')\">修改</a>" +
			   " | <a class=\"del_addr\" onclick=\"delAddr(this,'"+data.ids[i]+"')\">删除</a></div></div>";
			}
			else
				{
					 item = "<div class=\"item\" name='q_address'>" +
					   //"<div class=\"addr_id\" style=\"display:hidden;\">"+data.ids[i]+"</div>" +
					   "<div class=\"a\"></div>" + 
					   "<div class=\"c1\">"+data.addrs[i]+"</div>" +
					   "<div class=\"c2\">"+data.phones[i]+"</div>" +
					   "<div class=\"c3\"><a onclick=\"setDefault('"+data.ids[i]+"')\">设为默认</a></div>" +
					   "<div class=\"c4\"><a class=\"sel_addr\" onclick=\"editAddr(this,'"+data.ids[i]+"')\">修改</a>" +
					   " | <a class=\"del_addr\" onclick=\"delAddr(this,'"+data.ids[i]+"')\">删除</a></div></div>";
				}

			$('#showAddrList').append(item);
		});
	} else {
		$('.partr .title').html("您的地址列表为空");
	}
	$("#editAddrTable").hide();
    //re-bind the mouse events
	$('.addressList .item').mouseenter(function(){
		$(this).children('.a').show();
		$(this).children('.c3').show();
	});
	$('.addressList .item').mouseleave(function(){
		$(this).children('.a').hide();
		$(this).children('.c3').hide();
	});
}